#include "linear_linked_list.h"
#include "stdio.h"

void LinearLinkedListDemo(void)
{
    LinkedNode_t *head = LinkedListInit();

    BrowserData_t d1 = { "Google", "2025-07-15 10:00", "https://www.google.com" };
    BrowserData_t d2 = { "Baidu",  "2025-07-15 10:05", "https://www.baidu.com" };
    BrowserData_t d3 = { "OpenAI", "2025-07-15 10:10", "https://www.openai.com" };

    LinkedListInsertToHead(head, &d1);
    LinkedListInsertToTail(head, &d2);
    LinkedListInsertCom(head, 1, &d3);

    // 打印链表内容
    printf("单链表内容:\n");
    LinkedNode_t *node = head->next;
    while (node) {
        printf("Title: %s, Time: %s, URL: %s\n",
               node->browserData.title,
               node->browserData.timestamp,
               node->browserData.url);
        node = node->next;
    }

    printf("%d\n", LinkedListInsertCom(head, 3, &d1));

    // 查找
    printf("查找的节点内容:\n");
    LinkedNode_t *result = LinkedListSearchByIndex(head, 3);
    printf("Title: %s, Time: %s, URL: %s\n",
               result->browserData.title,
               result->browserData.timestamp,
               result->browserData.url);

    result = LinkedListSearchByData(head, &d3);
    printf("Title: %s, Time: %s, URL: %s\n",
               result->browserData.title,
               result->browserData.timestamp,
               result->browserData.url);

    // 删除第二个节点（索引从0开始，删除索引1）
    LinkedListDeleteByIndex(head, 1);

    // 打印删除后的链表
    printf("\n删除一个节点后的单链表:\n");
    node = head->next;
    while (node) {
        printf("Title: %s, Time: %s, URL: %s\n",
               node->browserData.title,
               node->browserData.timestamp,
               node->browserData.url);
        node = node->next;
    }

    // 销毁链表
    LinkedListDestroy(head);
}